package com.example.myapplication.View;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.Base.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.SQL.MDatabaseHelper;
import com.example.myapplication.model.Book;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class SaveDataActivity extends BaseActivity {

    private EditText inputData;
    private Button button1;
    private MDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.save_data_activit);
        inputData = (EditText) findViewById(R.id.input_content);
        button1 = (Button) findViewById(R.id.save_button);
//        button1.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View v){
//                String data = inputData.getText().toString();
//                save(data);
//            }
//        });
        dbHelper = new MDatabaseHelper(this, "BookStore", null ,2);
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                LitePal.getDatabase(); //现在用这个对象关系映射的开源数据库框架来完成这个工作
//                dbHelper.getWritableDatabase(); //之前使用SQLitedatabase方式创建和CRUD数据库
            }
        });
        Button button3 = (Button)findViewById(R.id.add_data);
        button3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Book book = new Book();
                book.setName("zoucheng");
                book.setAuthor("zoucheng");
                book.setPages(454);
                book.setPress("unkown");
                book.save();

//                SQLiteDatabase bd = dbHelper.getWritableDatabase();
//                ContentValues values = new ContentValues();
//                values.put("name", "the da vinci code");
//                values.put("author", "dan brown");
//                values.put("price", 454);
//                bd.insert("Book", null, values);
//                values.clear();
//                values.put("name", "the lost symbol");
//                values.put("author", "dan brown");
//                values.put("pages", 510);
//                values.put("price", 19.95);
//                bd.insert("Book", null, values);
            }
        });
        Button button4 = (Button)findViewById(R.id.update_data);
        button4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Book book = new Book();
                book.setName("yangming");
                book.setAuthor("yangming");
                book.setPages(100);
                book.setPress("unknow");
                book.updateAll("name= ? and author= ? ", "zoucheng", "zoucheng");
//                SQLiteDatabase db = dbHelper.getWritableDatabase();
//                ContentValues values = new ContentValues();
//                values.put("price", 1234);
//                Toast.makeText(SaveDataActivity.this, "updata success", Toast.LENGTH_LONG);
//                db.update("Book", values, "name = ?", new String[]{"the da vinci code"});
            }
        });
        Button button5 = (Button)findViewById(R.id.delete_data);
        button5.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                LitePal.deleteAll(Book.class, "pages < ?", "120");
//                SQLiteDatabase db = dbHelper.getWritableDatabase();
//                db.delete("Book", "pages > ?", new String[]{"500"});
            }
        });
        Button button6 = (Button)findViewById(R.id.query_data);
        button6.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                List<Book> books = LitePal.select("name", "author").find(Book.class);
                for (Book book : books){
                    Log.d("saveDataactivity", book.getName());
                }


//                SQLiteDatabase db = dbHelper.getWritableDatabase();
//                Cursor cursorRow = db.rawQuery("select * from Book", null);
//                Cursor cursor = db.query("Book", null, null, null, null, null, null);
//                if (cursor.moveToFirst()){
//                    long startTime = System.currentTimeMillis();
//                    do {
//                        String name = cursor.getString(cursor.getColumnIndex("name"));
//                        String author = cursor.getString(cursor.getColumnIndex("author"));
//                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
//                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
//                        Log.d("saveDataactivity", "book name is :" + name);
//                        Log.d("daveDataActivity", "book author is: " + author);
//                        Log.d("daveDataActivity", "book pages is: " + pages);
//                        Log.d("daveDataActivity", "book price is: " + price);
//                    }while(cursor.moveToNext());
//                    Log.d("totalTimeMill", (System.currentTimeMillis() - startTime) + "");
//                }
//                cursor.close();
            }
        });

    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        String inputText = inputData.getText().toString();
        save(inputText);
    }

    private void save(String data){
        FileOutputStream out = null;
        BufferedWriter writer  = null;
        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
            Toast.makeText(SaveDataActivity.this,"save data finish", Toast.LENGTH_LONG);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                if (writer != null){
                    writer.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

}
